package com.xingzhe.learning.service.Impl;

import com.alibaba.fastjson.JSON;
import com.xingzhe.base.model.RestResponse;
import com.xingzhe.content.model.po.CoursePublish;
import com.xingzhe.content.model.po.Teachplan;
import com.xingzhe.learning.feignclient.ContentServiceClient;
import com.xingzhe.learning.feignclient.MediaServiceClient;
import com.xingzhe.learning.model.dto.XcCourseTablesDto;
import com.xingzhe.learning.service.LearningService;
import com.xingzhe.learning.service.MyCourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 在线学习的接口
 */
@Slf4j
@Service
public class LearningServiceImpl implements LearningService {
    @Resource
    MyCourseTablesService myCourseTablesService;
    @Resource
    ContentServiceClient contentServiceClient;
    @Resource
    MediaServiceClient mediaServiceClient;
    @Override
    public RestResponse<String> getVideo(String userId, Long courseId, Long teachplanId, String mediaId) {
        //查询课程信息
        CoursePublish coursepublish = contentServiceClient.getCoursepublish(courseId);
        //判断有没有该课程
        if (coursepublish==null){
            return RestResponse.validfail("课程不存在");
        }

        //判断课程是否支持试学   远程调用内容管理服务根据课程计划id（teachplanId）去查课程计划信息，如果is_preview的值为1表示支持试学
        String teachplan1 = coursepublish.getTeachplan();
        List<Teachplan> teachplans = JSON.parseArray(teachplan1, Teachplan.class);
        for (Teachplan t:teachplans
             ) {
            Long id = t.getId();
            Teachplan teachplan = contentServiceClient.getOne(id);
            String isPreview = teachplan.getIsPreview();
            //为true则表示支持试学
            if (isPreview.equals("1")){
                //远程调用媒资获取视频播放地址
                RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return playUrlByMediaId;
            }
        }

        //用户已登录
        if (StringUtils.isNotEmpty(userId)){
            //通过我的课程表来查询有没有学习资格
            XcCourseTablesDto learning = myCourseTablesService.getLearningStatus(userId, courseId);
            String learnStatus = learning.getLearnStatus();
            if ("702002".equals(learnStatus)){
                return RestResponse.validfail("无法学习，因为没有选课或者选课后没有支付");
            }else if ("702003".equals(learnStatus)){
                return RestResponse.validfail("已过期需要申请续费或重新支付");
            }else {
                //有学习资格，返回视频播放地址
                //远程调用媒资获取视频播放地址
                RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
                return playUrlByMediaId;
            }
        }
        //如果用户没有登录
        //取出课程的收费资格
        String charge = coursepublish.getCharge();
        if ("201000".equals(charge)){
            //有学习资格，可以返回视频播放地址
            //远程调用媒资获取视频播放地址
            RestResponse<String> playUrlByMediaId = mediaServiceClient.getPlayUrlByMediaId(mediaId);
            return playUrlByMediaId;
        }

        return RestResponse.validfail("该课程还没有选课哦");
    }
}
